package com.LC.Service.Impl;

import com.LC.DAO.MemberDAO;
import com.LC.Service.MemberService;
import com.LC.Utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
  
    @Autowired
    MemberDAO memberDAO;
  
    @Override
    public Map<String, Object> MemberCountPerMonths() throws Exception {
      Map<String,Object> map = new HashMap<>();
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.MONTH,-12);
      List<String> months = new ArrayList();
      List<Integer> member = new ArrayList<>();
      Date time = calendar.getTime();
      String date2String = DateUtils.parseDate2String(time, "yyyy-MM");
      String beginDate = date2String + "-01";
      String endDate = date2String + "-31";
      months.add(date2String);
      try {
        for (int i = 0; i < 12; i++) {
          int numberPerMonth = memberDAO.findNumberPerMonth(beginDate, endDate);
          member.add(numberPerMonth);
          calendar.add(Calendar.MONTH,1);
          time = calendar.getTime();
          date2String = DateUtils.parseDate2String(time, "yyyy-MM");
          endDate = date2String + "-31";
          months.add(date2String);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      map.put("months",months);
      map.put("memberCount",member);
      return map;
    }
}
